﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ServiceDeskClient.ServiceDeskServiceReference;

namespace ServiceDeskClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            InitializeComponent();

            Model = new LoginModel() { Login = "adm" };
            this.DataContext = Model;
            this.Loaded+= (s,e)=>
                {
                    var client = new  ServiceDeskServiceReference.ServiceDeskServiceClient();
                    ChannelFactory = client.ChannelFactory;

                };
        }
        private class LoginModel
        {
            public string Login { get; set; }
            public string Password { get; set; }

        }
        private readonly LoginModel Model;

        private ChannelFactory<ServiceDeskServiceReference.IServiceDeskService> ChannelFactory;
        public Tuple<ServiceDeskServiceReference.IServiceDeskService, LoginDTO> Result { get; private set; }



        private void Login_Click(object sender, RoutedEventArgs e)
        {
            var s = ChannelFactory.CreateChannel();

            //try
            //{
                var task = new Task<LoginDTO>(()=>s.Login(Model.Login, Model.Password)) ;
                
                task.ContinueWith(_=>
                    {
                        if(_.IsFaulted)
                        {
                            var isWrongCredentialsError = _.Exception.InnerExceptions.OfType<FaultException<ModelFault>>().Any();
                            Dispatcher.SimpleInvoke(() =>
                                {
                                    if(isWrongCredentialsError)
                                        MessageBox.Show("Неверный логин или пароль");// TODO это строка должна быь локализована
                                    else
                                        MessageBox.Show("Ошибка");// TODO это строка должна быь локализована
                                });
                        }
                        else
                        {
                            Dispatcher.SimpleInvoke(()=>
                                {
                                    var login = _. Result;
                                    Result = Tuple.Create(s, login);
                                    this.Close();
                                });
                        }
                    });


                ProgressBar.RunWithProgressBar( task);
                    


                //var login = s.Login(Model.Login, Model.Password);
                //Result = Tuple.Create(s, login);
                //this.Close();
            //}
            //catch (FaultException<ModelFault>)
            //{
            //    MessageBox.Show("Неверный логин или пароль");// TODO это строка должна быь локализована
            //    (s as ICommunicationObject).Close();
            //}
            //catch
            //{
            //    (s as ICommunicationObject).Close();
            //}            

        }
    }
}
